package edu.cdnu.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.cdnu.dao.EducationalAdminDao;
import edu.cdnu.pojo.EducationalAdminPojo;
import edu.cdnu.service.EducationalAdminService;

@Service
public class EducationalAdminServiceImpl implements EducationalAdminService {

	@Autowired
	private EducationalAdminDao educationalAdminDao;
	
	@Override
	public List<EducationalAdminPojo> selectAllEducationAdminList() {
		return educationalAdminDao.selectAllEducationAdminList();
	}

	@Override
	public void addEducationalAdmin(EducationalAdminPojo educationalAdmin) {
		// 先判空，这里账号，密码，姓名为必须字段
		if(educationalAdmin != null 
				// 这里判空对象是可以用!=null的，后面空字符就需要空字符写在equals前面
				&& educationalAdmin.getAccount() != null && !"".equals(educationalAdmin.getAccount())
				&& educationalAdmin.getName() != null && !"".equals(educationalAdmin.getName())
				) {
			// 判断密码,为空给默认值
			if(educationalAdmin.getPassword() == null || "".equals(educationalAdmin.getPassword())) {
				educationalAdmin.setPassword("123456");
			}
			// 先把密码加密  用账号作盐
			Md5Hash md5=new Md5Hash(educationalAdmin.getPassword(),educationalAdmin.getAccount());
			educationalAdmin.setPassword(md5.toString());
			// 新增加密后的用户信息
			educationalAdminDao.addEducationalAdmin(educationalAdmin);
		}else {
			return ;
		}
	}

	@Override
	public void updateEducationalAdmin(EducationalAdminPojo educationalAdmin) {
		if(educationalAdmin != null && educationalAdmin.getId() != null) {
			educationalAdminDao.updateEducationalAdmin(educationalAdmin);
		}else {
			return ;
		}
		
	}

}
